//
//  ReComtItemView.m
//  JiaoX
//
//  Created by yinqy on 15/1/9.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ReComtItemView.h"

#define FONT_SIZE [DimensUtils getScaleX:12]

@interface ReComtItemView()
{
    UILabel *_contentLabel;
    CGFloat _width;
    UIView *_spliteView;
}
@end

@implementation ReComtItemView
- (instancetype)initWithWidth:(CGFloat)width{
    self = [super init];
    if (self) {
        _width = width;
        _spliteView = [[UIView alloc]init];
        [_spliteView setBackgroundColor:JXColor(229, 229, 229)];
        [self addSubview:_spliteView];
        
        _contentLabel = [[UILabel alloc]init];
        [_contentLabel setTextColor:JXColor(152, 152, 152)];
        [_contentLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [self addSubview:_contentLabel];
        
    }
    return self;
}

-(void)setComt:(CommentInfo *)comt{
    _comt = comt;
    
    UIColor *color1 = JXColor(102, 102, 102);//名称的颜色
    UIColor *color2 = JXColor(51, 51, 51);//回复内容的颜色
    UIColor *color3 = JXColor(153, 153, 153);
    NSInteger index = 0;
    
    NSMutableDictionary *rangs = [NSMutableDictionary dictionary];
    
    NSMutableString *content = [[NSMutableString alloc] initWithString:comt.commentName];
    [content appendString:@"："];
    
    //设置名称的rang
    index = index + content.length;
    [rangs setObject:color1 forKey:NSStringFromRange(NSMakeRange(0, index))];
    
    if (![Utils isBlankString:comt.userNameOther]) {
        // 代表这条评论是回复别人的，需要拼装字符串
        [content appendFormat:@"回复@%@：",comt.userNameOther];
        
        //设置“回复@”这三个字的rang
        [rangs setObject:color2 forKey:NSStringFromRange(NSMakeRange(index, 3))];
        index = index + 3;
        
        //设置被回复人的名字的rang
        [rangs setObject:color1 forKey:NSStringFromRange(NSMakeRange(index, comt.userNameOther.length + 1))];
        index = index + comt.userNameOther.length + 1;

    }
    [content appendFormat:@"%@　%@",comt.commentContent,comt.commentTime];
    
    //设置内容的rang
    [rangs setObject:color2 forKey:NSStringFromRange(NSMakeRange(index, comt.commentContent.length))];
    index = index + comt.commentContent.length;
    
    //设置时间的rang
    [rangs setObject:color3 forKey:NSStringFromRange(NSMakeRange(index, content.length-index))];
    
    CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(_width-10, MAXFLOAT)];
    self.size = CGSizeMake(_width, size.height+JXPublicInset*1.7);
    _contentLabel.size = CGSizeMake(_width-10, self.height);
    _spliteView.frame = CGRectMake(0, self.height-0.5, _width, 0.5);
    
    [_contentLabel setNumberOfLines:0];
    [_contentLabel setAttributedText:[Utils getAttribtString:content attrRangs:rangs]];
}

+(CGFloat)getHeightWithComt:(CommentInfo *)comt width:(CGFloat)width{
    NSMutableString *content = [[NSMutableString alloc] initWithString:comt.commentName];
    [content appendString:@"："];
    if (![Utils isBlankString:comt.userNameOther]) {
        // 代表这条评论是回复别人的，需要拼装字符串
        [content appendFormat:@"回复@%@：",comt.userNameOther];
    }
    [content appendFormat:@"%@　%@",comt.commentContent,comt.commentTime];
    CGSize size =[content sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(width-10, MAXFLOAT)];
    return size.height+JXPublicInset*1.7;
}


@end
